/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
 */

package com.ewerp.mud.core.session.parsers;

import com.ewerp.mud.commands.CreateRoomCommand.CreateRoomTerminalLinePacketParser;
import com.ewerp.mud.commands.ICommand;
import com.ewerp.mud.commands.SayCommand.SayCommandTerminalLinePacketParser;
import com.ewerp.mud.core.session.parsers.TerminalLineParser.ITerminalLinePacketParser;
import com.ewerp.mud.entities.IEntity;

/**
 * A parsing engine for the Terminal Line Parser format. See the sub interface
 * {@link ITerminalLinePacketParser} for details in the Terminal Line Parser
 * protocol.
 * 
 * @author cboyden
 * 
 */
public class TerminalLineParser {
    /**
     * Parses a Terminal Line Packet and generates an {@link ICommand}.
     * 
     * The Terminal Line Packet format is a single line string found in
     * traditional mud telnet communications. (e.g.- get my sword from my
     * sheath).
     * 
     * The implementor may need to know about the invoker to determine what
     * objects the Terminal Line Packet is referring to.
     * 
     * @author cboyden
     * 
     */
    public static interface ITerminalLinePacketParser {
        public ICommand parseTerminalLinePacket(IEntity source, String packet);
    }

    protected static TerminalLineParser instance = new TerminalLineParser();

    protected TerminalLineParser() {
    }

    public static TerminalLineParser getInstance() {
        if (instance == null) {
            instance = new TerminalLineParser();
        }
        return instance;
    }

    public ICommand processPacket(String packet) {
        ICommand result = null;

        if (packet.startsWith("say")) {
            // TODO: Generate a Say command
            ITerminalLinePacketParser tlpp = new SayCommandTerminalLinePacketParser();
            result = tlpp.parseTerminalLinePacket(null, packet);
        } else if(packet.startsWith("createroom")) {
            // TODO: Generate a create command
            ITerminalLinePacketParser tlpp = new CreateRoomTerminalLinePacketParser();
            result = tlpp.parseTerminalLinePacket(null, packet);
        }

        return result;
    }
}
